package chapter06._02_Lamba;

import java.time.Instant;
import java.util.Arrays;
import javax.swing.*;

/**
 * @Version:
 * @Description: lamba表达式的学习
 * @LastDate: 2020/9/28
 */
public class LambaTest {
    /*
     总结：
     lamba说到底还是对代码的复用 使用方法想象成和编写方法一样就行 有点临时方法的味道
     */
    public static void main(String[] args) {
        var s = " 123 ";
        System.out.println();
    }

    public static void whatIsLamba() {
        //what is lamba expression
        //it is a code block
        //a way to pass code block
        //ex.1 Arrays 's sort
        var planets = new String[]{"Merucy", "Venus", "Earth", "Mars",
                                "Jupiter", "Staturn"};
        System.out.println("Before sort:\n" + Arrays.toString(planets));
        System.out.println("Sorted in Dictionary order:");
        Arrays.sort(planets);
        System.out.println(Arrays.toString(planets));
        System.out.println("Sorted By length:");
        //Arrays.sort(planets, (s1, s2) -> s1.length() - s2.length());//不带中括号写法
        Arrays.sort(planets, (s1, s2) -> {
            return s1.length() - s2.length();
        });//如果一个表达式无法写完 就可以放在{}代码块中 并包含return语句
        System.out.println(Arrays.toString(planets));

        //lamba also can transform to interface expression, and only can to interface
        var timer = new Timer(1000, event -> {
            System.out.println(Instant.ofEpochMilli(event.getWhen()));
        });
        timer.start();

        JOptionPane.showMessageDialog(null, "Quit?");
        System.exit(0);
    }
}
